if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000);
// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

function task() {
  var 首页 = text("首页").findOne(30000)
  if (首页) {
    sleep(8000)    
    utils.swip_up();
    utils.updateAppProgress(10);
    sleep(time)
    var 金币奖励 = textContains("今日金币奖励").findOne(5000)
    if (金币奖励) {
      var money = parseFloat(金币奖励.text().substring(7, 14))
      if (money <= 0) {
        toastLog("开始眼保健操")
        utils.updateAppProgress(30);
        var 开始 = text('开始').findOne(5000)
        if (开始) {
          点击控件(开始)
          var 取消 = text('取消').findOne(5000)
          if (取消) {
            toastLog('当前是验证界面')
          } else {
            sleep(random(310000, 320000))
          }
          utils.updateAppProgress(50);
        }
        var 停止 = text('停止').findOne(5000)
        if (停止) {
          点击控件(停止)
          sleep(time)
        }
      } else {
        toastLog("您今天已经做过眼保健操了")
      }
    }
  } else {
    toastLog('没进去首页，任务运行失败')
  }
}

function main() {
  let open_app = launchAppEnhance("兼职视界");
  if (open_app) {
    task()
  }
  else {
    toastLog("兼职视界打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}



storage.put("script_running_status", "end");